<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i> 商品列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" round @click="urlAdd"><i class="el-icon-plus"> </i>&nbsp;&nbsp;新增商品</el-button>
            <div style="height:40px"></div>
                <el-form :inline="true"  class="demo-form-inline">
                    <el-form-item label="商品标题:">
                        <el-input v-model="title" placeholder="请输入商品标题" style="width:300px;"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSelect"> &nbsp;&nbsp;查询&nbsp;&nbsp;  </el-button>
                        <!-- <el-button type="primary" @click="onResetting"> &nbsp;&nbsp;重置&nbsp;&nbsp;  </el-button> -->
                    </el-form-item>
                </el-form>
                <el-table :data="tableData"  style="width: 100%"   ref="multipleTable">
                    <el-table-column prop="id" label="ID" width="50" align="center">
                    </el-table-column>
                    <el-table-column prop="name" label="商品标题" >
                    </el-table-column>
                    <el-table-column prop="category_level1_name" label="一级分分类" width="120" align="center">
                    </el-table-column>
                    <el-table-column prop="category_level2_name" label="二级分类" width="150" align="center">
                    </el-table-column>
                    <el-table-column prop="discount_price" label="优惠价" width="150" align="center" >
                    </el-table-column>
                    <el-table-column prop="price" label="原价" width="150" align="center">
                    </el-table-column>
                    <el-table-column prop="spec" label="规格"  width="150" align="center">
                    </el-table-column>
                    <el-table-column prop="create_time" label="发布时间"  width="180" align="center">
                    </el-table-column>
                    <el-table-column label="操作" width="200" align="center" >
                        <template slot-scope="scope" >
                            <el-button size="small" type="text" @click="handleSel(scope.$index, scope.row)" >查看</el-button>
                            <el-button class="edit" size="small" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button class="del" size="small" type="text"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                            <el-button class="download" size="small" type="text"  @click="handleDownload(scope.$index, scope.row)">下载小程序码</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination" v-show="totalPages>1">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :page-size="pageSize" :total="totalElements" background>
                    </el-pagination>
                </div>
                
        </div>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除提示框 -->
    </div>
</template>
<script>
export default {
    name:"goods",
    data() {
        return {
            pageIndex:1,//当前页码
            pageSize:10,//每页10条
            totalElements:1,//默认的总条数
            totalPages:1,//默认的总页数
            tableData:[],//表格数据
            delIndex:0, //当前选中的下标
            delId:0,  //当前要删除的Id
            delVisible:false, //默认dialog 不弹出
            title:'', //商品标题

        }
    },
    //实例创建
    created(){
        //
        this.getDate();
    },
    //计算属性
    computed:{
        
    },
    //方法
    methods: {
        //获取数据
        getDate(){
            const querystring = require('querystring');
            const param ={
                title:this.title,//标题
                pageIndex:this.pageIndex,
                pageSize:this.pageSize
            }
            this.$axios.post('/manage/goods/list',querystring.stringify(param))
            .then((response)=> {
                // console.log(response);
                if(response.data.code==200){
                    var data = response.data.data;//当前返回的数据
                    this.tableData = data.currData;//列表数据
                    this.totalElements = data.totalElements; //总条数
                    this.totalPages = data.totalPages;//总页数
                }else{
                    this.$notify.error({
                        title: '请求失败',
                        message: response.data.msg
                    });
                }
            })
            .catch((error)=> {
                console.log(error);
            });
        },
        //新增商品
        urlAdd(){
            this.$router.push({ path:'/goods/add'})
        },
        //查询
        onSelect(){
            this.pageIndex =1;
            this.getDate();
        },
        //分页
        handleCurrentChange(val){
            this.pageIndex =val;
            this.getDate();
        },
        //查看
        handleSel(index,row){
            if(row.id && row.id>0){
                //跳转到商品详情
                this.$router.push({ path: '/goods/select/'+row.id });
            }
        },
        //编辑
        handleEdit(index,row){
            if(row.id && row.id>0){
                //跳转到商品编辑
                this.$router.push({ path: '/goods/edit/'+row.id });
            }
        },
        //删除
        handleDelete(index,row){
            // console.log(index,row.id);
            if(row.id && row.id>0){
                this.delIndex =index;//要删除的下标
                this.delId = row.id; //要删除的ID
                this.delVisible = true; //显示兑换弹出框
                 //console.log(this.delVisible);
            }else{
                // console.log("没触发");
            }
        },
        // 确定删除
        deleteRow(){
            //console.log(this.delId)
            if(this.delId>0){
                const querystring = require('querystring');
                const param ={
                    goodsId:this.delId
                }
                this.$axios.post('/manage/goods/delete',querystring.stringify(param)).then(response=>{
                    //console.log(response);
                    if(response.data.code==200){
                        this.tableData.splice(this.delIndex, 1);
                        this.$message.success('删除成功');
                        this.delVisible = false;
                    }else{
                        this.$notify.error({
                            title: '删除失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    
                }).catch(error=>{
                    console.log(error); 
                });
            }
            
        },
        //跳转下载小程序二维码
        handleDownload(index,row){
            if(row.id&&row.id>0){
                window.open("http://api.xfkka.com/manage/qrcode/download/goods?goodsId="+row.id);   
            }
        }
        
    }
}
</script>
<style lang="stylus" scoped>
    .edit{
        color #ff9800;
    }
    .del{
        color #e51c23;
    }
    .download{
        color #67C23A;
    }
</style>


